//
//  NetHandler.m
//  Gift
//
//  Created by dllo on 15/9/16.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "NetHandler.h"
// MD5
#import <CommonCrypto/CommonDigest.h>


@implementation NetHandler



+ (void)dataWithUrl:(NSString *)str complant:(void (^)(id data))block
{
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HUDHidden" object:@"0"];
    
    //    NSLog(@"%@",NSHomeDirectory());
    // wifi 加载菊花
    UIApplication *application = [UIApplication sharedApplication];
    application.networkActivityIndicatorVisible = YES;
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 可以停止加载
    AFNetworkReachabilityManager *reach = [AFNetworkReachabilityManager sharedManager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    
    str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];

    const char *cStr = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [hash appendFormat:@"%02X",result[i]];
    }

    [manager GET:str parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        
        NSData *data = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
        //拼接一个文件路径
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        //        NSString *string = [NSString stringWithFormat:@"%ld.txt",num];
        NSString *path = [docPath stringByAppendingPathComponent:hash];
        
        //数据写入本地
        [data writeToFile:path atomically:YES];
        
        block(responseObject);
        
        application.networkActivityIndicatorVisible = NO;
        
        [reach stopMonitoring];
        

        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"HUDHidden" object:@"1"];

    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        NSLog(@"error");

        //拼接一个文件路径
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        
        NSString *path = [docPath stringByAppendingPathComponent:hash];
        
        NSData *data = [NSData dataWithContentsOfFile:path];
        
            if (data != nil) {
                id idData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                
                block(idData);
                
        }
        
        
  
        
        
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"HUDHidden" object:@"1"];
        application.networkActivityIndicatorVisible = NO;
    }];
}
+ (void)nilHUDdataWithUrl:(NSString *)str complant:(void (^)(id data))block
{
    //    NSLog(@"%@",NSHomeDirectory());
    // wifi 加载菊花
    UIApplication *application = [UIApplication sharedApplication];
    application.networkActivityIndicatorVisible = YES;
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 可以停止加载
    AFNetworkReachabilityManager *reach = [AFNetworkReachabilityManager sharedManager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    
    str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    
    const char *cStr = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [hash appendFormat:@"%02X",result[i]];
    }
    
    [manager GET:str parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        
        NSData *data = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
        //拼接一个文件路径
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        //        NSString *string = [NSString stringWithFormat:@"%ld.txt",num];
        NSString *path = [docPath stringByAppendingPathComponent:hash];
        
        //数据写入本地
        [data writeToFile:path atomically:YES];
        
        block(responseObject);
        
        application.networkActivityIndicatorVisible = NO;
        
        [reach stopMonitoring];
       
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        NSLog(@"error");
        
        //拼接一个文件路径
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        
        NSString *path = [docPath stringByAppendingPathComponent:hash];
        
        NSData *data = [NSData dataWithContentsOfFile:path];
        
        if (data != nil) {
            id idData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            block(idData);
            
        }
    
        application.networkActivityIndicatorVisible = NO;
    }];
}
+ (void)nilHUDdataWithUrl:(NSString *)str fileName:(NSString *)pathName complant:(void (^)(id data , NSString * pathName))block
{
    //    NSLog(@"%@",NSHomeDirectory());
    // wifi 加载菊花
    UIApplication *application = [UIApplication sharedApplication];
    application.networkActivityIndicatorVisible = YES;
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 可以停止加载
    AFNetworkReachabilityManager *reach = [AFNetworkReachabilityManager sharedManager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    
    str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    
    const char *cStr = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [hash appendFormat:@"%02X",result[i]];
    }
    
    [manager GET:str parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        
        NSData *data = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
        //拼接一个文件路径
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        //        NSString *string = [NSString stringWithFormat:@"%ld.txt",num];
        NSString *path = [docPath stringByAppendingPathComponent:hash];
        
        //数据写入本地
        [data writeToFile:path atomically:YES];
        
        block(responseObject,pathName);
        
        application.networkActivityIndicatorVisible = NO;
        
        [reach stopMonitoring];
        
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        NSLog(@"error");
        
        //拼接一个文件路径
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        
        NSString *path = [docPath stringByAppendingPathComponent:hash];
        
        NSData *data = [NSData dataWithContentsOfFile:path];
        
        if (data != nil) {
            id idData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            block(idData,pathName);
            
        }
        
        application.networkActivityIndicatorVisible = NO;
    }];
}


- (NSString *)md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [hash appendFormat:@"%02X",result[i]];
    }
    return hash;
}


@end
